package com.hy.convenient.api.controller.down;

import com.hy.convenient.api.controller.conInterface.DownControllerInterface;
import com.hy.convenient.api.pojo.QueryUpdatePojo;
import com.hy.convenient.api.pojo.UserDownPojo;
import com.hy.convenient.api.pojo.VipLevel;
import com.hy.convenient.api.pojo.check.Check;
import com.hy.convenient.api.pojo.check.CheckUpdateCheckPojo;
import com.hy.convenient.api.pojo.check.VipDateCheckPojo;
import com.hy.convenient.api.server.CheckUpdateServer;
import com.hy.convenient.api.server.VipLevelServer;
import com.hy.convenient.common.LayuiToTable;
import com.hy.convenient.common.TableNameEnum;
import com.hy.convenient.tool.DateUitl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @className VipLevelControllor
 * @Author hdy
 * @Date 2021-1-21 14:24
 * @Version 1.0
 */
@RestController
public class VipLevelControllor implements DownControllerInterface<VipDateCheckPojo> {
    @Autowired
    private VipLevelServer vipLevelServer;
    @Autowired
    private CheckUpdateServer checkUpdateServer;
    @Override
    @PostMapping("/api/vipLevelDown")
    public Map<String, Object> downData(@RequestBody VipDateCheckPojo t) {
//        String str = check(t);
//        if (!StringUtils.isEmpty(str)) {
//            return LayuiToTable.business_fail(str);
//        }
//        if (!MD5.check(t.getMerId() + t.getPage() + t.getCwbm()+t.getDateTime(), t.getSign())) {
//            return LayuiToTable.business_fail("签名失败");
//        }
        if(t.getPage() ==0){
            t.setPage(1);
        }
        Map<String, Object> map = new HashMap();
        map.put("cwbm", t.getCwbm());
        map.put("djbm", t.getDjbm());
        map.put("merId", t.getMerId());
        map.put("pageSize", t.getPageSize());
        map.put("starSize", (t.getPage() -1)*t.getPageSize());
        int count = vipLevelServer.queryCount(map);
        List<VipLevel> list = vipLevelServer.queryDownList(map);
        return LayuiToTable.layuiToTable( list,count);
    }

    @Override
    @PostMapping("/api/updateVipLevel")
    public Map<String, Object> checkUpdateDateDown(@RequestBody CheckUpdateCheckPojo t) {
//        String str = checkUpdate(t);
//        if (!StringUtils.isEmpty(str)) {
//            return LayuiToTable.business_fail(str);
//        }
//        if (!MD5.check(t.getMerId()  + t.getCwbm()+t.getDateTime(), t.getSign())) {
//            return LayuiToTable.business_fail("签名失败");
//        }
        if(StringUtils.isEmpty(t.getLastTime())){
            t.setLastTime( DateUitl.getDateString(DateUitl.fromat_01));
        }
        Map<String, Object> map = new HashMap();
        map.put("cwbm", t.getCwbm());
        map.put("merId", t.getMerId());
        map.put("pageSize", t.getPageSize());
        map.put("starSize", (t.getPage() -1)*t.getPageSize());
        map.put("lastUpateTime", t.getLastTime());
        map.put("nowTime", t.getNowTime());
        map.put("lsdh", t.getCwbm());
        map.put("tableName", TableNameEnum.KHZX_HYKDJ.getTableName());
        int count = checkUpdateServer.queryCount(map);
        List<QueryUpdatePojo> list = checkUpdateServer.queryDownList(map);
        List<String> delete = new ArrayList<>();
        List<String> insertUpdate = new ArrayList<>();
        /**
         * 一下解析数据完全是更具当前的数据库，硬解析
         */
        list.stream().filter(c->StringUtils.isNotEmpty(c.getPrimaryKey())).forEach(c->{
            String primaryKey =  c.getPrimaryKey();
            int star = primaryKey.indexOf("'");
            int end = primaryKey.indexOf("'",star+1);
            if (c.getStatus().toLowerCase().trim().equals("delete")) {
                delete.add(primaryKey.substring(star + 1, end));
            } else {
                insertUpdate.add(primaryKey.substring(star + 1, end));
            }
        });
        insertUpdate.removeAll(delete);
        List<VipLevel> insert = new ArrayList<>();;
        if(!insertUpdate.isEmpty()){
            map.clear();
            map.put("merId", t.getMerId());
            map.put("list", insertUpdate);
            insert = vipLevelServer.queryUpdateDownList(map);
        }

        map.clear();
        map.put("insert",insert);
        map.put("delete",delete);
        List<Map<String, Object>> lists=new ArrayList<>();
        lists.add(map);
        return LayuiToTable.layuiToTable(lists ,count);
    }
}

